﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Global;
using SIMS.Database;
using System.Collections;
using System.Data;

namespace SIMS.School
{
    public class Room
    {
        public int Id { get; set; }
        public int CompanyId { get; set; }

        public string CompanyName { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public Int16 SeatNo { get; set; }
        public RoomType TypeOfRoom { get; set; }
        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }
    }

    public enum RoomType
    {
        NotClassfied = 0,
        Class = 1,
        Lab = 2,
        Hall = 3,
        Departmental = 4,
        WashOrToilet = 5,
        Tool = 6,
        Meeting = 7,
        Warehouse = 8,
        Hostel = 9,
        Shop = 10,
        Others = 99
    }

    public class RoomList : List<Room>
    {
        public MyError Error { get; set; }
        public RoomList()
        {
        }
    }

    public class RoomDB
    {
        MyDatabase _db = new MyDatabase();

        public Room Save(Room Room)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", Room.Id);
                _param.Add("@CompanyId", Room.CompanyId);

                if (Room.Description != string.Empty)
                {
                    _param.Add("@Description", Room.Description);
                }
                else
                {
                    _param.Add("@Description", DBNull.Value);
                }

                _param.Add("@Name", Room.Name);

                if (Room.TypeOfRoom >= 0)
                {
                    _param.Add("@TypeOfRoom", Convert.ToByte(Room.TypeOfRoom));
                }
                else
                {
                    _param.Add("@TypeOfRoom", DBNull.Value);
                }

                if (Room.SeatNo > 0)
                {
                    _param.Add("@SeatNo", Room.SeatNo);
                }
                else
                {
                    _param.Add("@SeatNo", DBNull.Value);
                }

                _param.Add("@IsActive", Room.IsActive);

                int _affRow = _db.ExecuteNonSqlSP("usp_RoomInsertUpdateSingleItem", _param);

                if (_affRow > 0)
                {
                    Room.Error.ErrorNo = 0;
                }
                else
                {
                    Room.Error.ErrorNo = 1;
                    Room.Error.ErrorMessage = "No row affect when saving school's Room details.";
                }
            }
            catch (Exception Ex)
            {
                Room.Error.ErrorNo = 10002;
                Room.Error.ErrorLocation = "Room";
                Room.Error.ErrorMessage = Ex.Message;
                Room.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Room");
            }

            return Room;
        }

        public RoomList GetRoomList(int CompanyId)
        {
            RoomList retValue = new RoomList();

            try
            {

                DataTable _dt = new DataTable("tblRoom");

                SortedList _param = new SortedList();
                _param.Add("@CompanyId", CompanyId);

                _dt = _db.GetDataTableBySP("usp_RoomSelectItemList", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        Room r = new Room();
                        FillData(_dr, r);
                        retValue.Add(r);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Room";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Room");
            }

            return retValue;
        }

        public Room GetRoom(int Id, int CompanyId)
        {
            Room retValue = new Room();

            try
            {

                DataTable _dt = new DataTable("tblRoom");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _param.Add("@CompanyId", CompanyId);

                _dt = _db.GetDataTableBySP("usp_RoomSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Room";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Room");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, Room Room)
        {
            Room.Id = Convert.ToInt32(aRow["Id"].ToString());
            Room.CompanyId = Convert.ToInt32(aRow["CompanyId"].ToString());
            
            Room.Error = new MyError();

            if (aRow["Description"] != DBNull.Value)
            {
                Room.Description = aRow["Description"].ToString();
            }
            else
            {
                Room.Description = string.Empty;
            }

            Room.Name = aRow["Name"].ToString();
            Room.CompanyName = aRow["CompanyName"].ToString();

            if (aRow["TypeOfRoom"] != DBNull.Value)
            {
                Room.TypeOfRoom = (RoomType)Enum.Parse(typeof(RoomType), aRow["TypeOfRoom"].ToString());
            }
            else
            {
                Room.TypeOfRoom = RoomType.NotClassfied;
            }

            if (aRow["SeatNo"] != DBNull.Value)
            {
                Room.SeatNo = Int16.Parse(aRow["SeatNo"].ToString());
            }
            else
            {
                Room.SeatNo = 0;
            }

            Room.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            Room.Error.ErrorNo = 0;
        }
    }
}
